##############################################################################
#  Build Orchestrator
###############################################################################

FROM golang:1.14.10-alpine3.12 as builder-orc

RUN set -ex \
    && apk add --no-cache \
        bash gcc git musl-dev openssl rsync perl-utils

ARG ORCHESTRATOR_VERSION=v3.2.3
ARG ORCHESTRATOR_REPO=https://github.com/openark/orchestrator.git
RUN set -ex \
    && mkdir -p $GOPATH/src/github.com/openark/orchestrator \
    && cd $GOPATH/src/github.com/openark/orchestrator \
    && git init && git remote add origin $ORCHESTRATOR_REPO \
    && git fetch --tags \
    && git checkout $ORCHESTRATOR_VERSION

WORKDIR $GOPATH/src/github.com/openark/orchestrator

RUN set -ex && ./build.sh -b -P


###############################################################################
#  Docker image for orchestrator
###############################################################################

FROM alpine:3.12

# Create a group and user
RUN addgroup -g 777 orchestrator && adduser -u 777 -g 777 -S orchestrator

ENV DOCKERIZE_VERSION v0.6.1
RUN set -ex \
    && apk add --update --no-cache \
        curl \
        wget \
        tar \
        openssl \
    && mkdir /etc/orchestrator /var/lib/orchestrator \
    && chown -R 777:777 /etc/orchestrator /var/lib/orchestrator \
    && wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-alpine-linux-amd64-$DOCKERIZE_VERSION.tar.gz -O- | \
        tar -C /usr/local/bin -xzv

COPY --chown=777:777 rootfs/ /
COPY --from=builder-orc /tmp/orchestrator-release/build/orchestrator/usr/local/orchestrator/ /usr/local/orchestrator/

USER 777
EXPOSE 3000 10008
VOLUME [ "/var/lib/orchestrator" ]

ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["/usr/local/bin/orchestrator", "-quiet", "-config", "/etc/orchestrator/orchestrator.conf.json", "http"]
